package top.lauwen.xwen;

/**
 * Created by Xwen on 2017/10/6.
 */

import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class TabContentFragment extends Fragment {

    private ListView list;
    private ArrayList<Music> musicArrayList;
    //当前歌曲序号，下标从零开始
    private int number = 0;
    //媒体播放类
    private MediaPlayer player = new MediaPlayer();
//    private ImageButton imgBtnPlay;


    public static TabContentFragment newInstance(){
//        Bundle arguments = new Bundle();
//        arguments.putString(EXTRA_CONTENT, content);
        TabContentFragment tabContentFragment = new TabContentFragment();
//        tabContentFragment.setArguments(arguments);
        return tabContentFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_tab_content, null,false);
        list = (ListView) contentView.findViewById(R.id.localMusic);
//        imgBtnPlay = (ImageButton) contentView.findViewById(R.id.controlBtn);


        initMusicList();
        initListView();
//        checkMusicfile();
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                number = position;
                //引入父Activity,从而实现对父Activity方法的调用
                MainActivity parentActivity = (MainActivity ) getActivity();
                parentActivity.play(number);
//                imgBtnPlay.setBackgroundResource(R.drawable.pause);

                parentActivity.setBackground();
            }
        });
        return contentView;
    }

    /**
     * 初始化音乐列表对象
     */
    private void initMusicList() {
        musicArrayList = MusicList.getMusicList();
        //避免重复添加音乐
        if(musicArrayList.isEmpty())
        {
            Cursor mMusicCursor = getActivity().getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[] { MediaStore.Audio.Media.TITLE,
                            MediaStore.Audio.Media.DURATION,
                            MediaStore.Audio.Media.ALBUM,
                            MediaStore.Audio.Media.ARTIST,
                            MediaStore.Audio.Media._ID,
                            MediaStore.Audio.Media.DATA,
                            MediaStore.Audio.Media.DISPLAY_NAME }, null, null,
                    MediaStore.Audio.AudioColumns.TITLE);

            if(mMusicCursor != null) {
                //标题
                int indexTitle = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);
                //艺术家
                int indexArtist = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);
                //总时长
                int indexTotalTime = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION);
                //路径
                int indexPath = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);

                /**通过mMusicCursor游标遍历数据库，并将Music类对象加载到ArrayList中*/
                for (mMusicCursor.moveToFirst(); !mMusicCursor.isAfterLast(); mMusicCursor
                        .moveToNext()) {
                    String strTitle = mMusicCursor.getString(indexTitle);
                    String strArtist = mMusicCursor.getString(indexArtist);
                    String strTotoalTime = mMusicCursor.getString(indexTotalTime);
                    String strPath = mMusicCursor.getString(indexPath);

                    if (strArtist.equals("<unknown>"))
                        strArtist = "无艺术家";
                    Music music = new Music(strTitle, strArtist, strPath, strTotoalTime);
                    musicArrayList.add(music);
                }
            }
        }
    }
    /**
     * 设置适配器并初始化ListView
     */
    private void initListView() {
        List<Map<String, String>> list_map = new ArrayList<Map<String, String>>();
        HashMap<String, String> map;
        SimpleAdapter simpleAdapter;
        for (Music music : musicArrayList) {
            map = new HashMap<String, String>();
            map.put("musicName", music.getMusicName());
            map.put("musicArtist", music.getMusicArtist());
            list_map.add(map);
        }

        String[] from = new String[] { "musicName", "musicArtist" };
        int[] to = { R.id.activity_tv_title_item, R.id.activity_tv_artist_item };

        simpleAdapter = new SimpleAdapter(getActivity(), list_map, R.layout.listitem,from, to);
        list.setAdapter(simpleAdapter);
    }
}